/**
 * EditPictureServiceImp.java
 *
 *
 * $LastChangedBy: allen $
 * $LastChangedDate: 2007-12-03 12:17:41 +0800 $
 * $Revision: 1324 $
 */
package com.myspace.service.picture;

import java.util.LinkedList;
import java.util.List;

import com.myspace.dao.PictureCommentDao;
import com.myspace.dao.PictureDao;
import com.myspace.dao.TagDao;
import com.myspace.dao.UserDao;
import com.myspace.domain.Picture;
import com.myspace.domain.PictureComment;
import com.myspace.domain.Tag;
import com.myspace.domain.User;
import com.myspace.common.exception.SystemException;


public class EditPictureServiceImp implements EditPictureService{

	private UserDao userDao;

	private PictureDao picDao;

	private TagDao tagDao;

	private PictureCommentDao picCommentDao;

	public void addPicture(Long userId, Picture picture, String[] tags) {
		User user = userDao.findById(userId);
		if (user == null)
			throw new SystemException("User is not exists!");
		if (tags != null && tags.length > 0) {

			List<Tag> tagList = tagDao.findTag(tags);
			List<Tag> addedTagList = new LinkedList<Tag>();
			List<Tag> removedTagList = new LinkedList<Tag>();

			//Calculate tags to remove from the picture.
			for (Tag tag : picture.getTags()) {
				boolean isRemoved = true;
				for (String tagName : tags) {
					if (tag != null && tag.getName().equals(tagName)) {
						isRemoved = false;
						break;
					}
				}
				if (isRemoved) {
					removedTagList.add(tag);
				}
			}
			//Remove tags from picture.
			for( Tag tag: removedTagList){
				picture.getTags().remove(tag);
			}

			//Calculate tags to add to Tag table.
			for (String tagName : tags) {
				boolean isExisted = false;
				for (Tag tag : tagList) {
					if (tag != null && tag.getName().equals(tagName)) {
						isExisted = true;
						break;
					}
				}
				if (!isExisted) {
					Tag newTag = new Tag();
					newTag.setName(tagName);
					addedTagList.add(newTag);
				}
			}
			//Add tag to picture.
			for (Tag tag : addedTagList) {
				tag = tagDao.save(tag);
				picture.getTags().add(tag);
			}
			for (Tag tag : tagList) {
				if (!picture.getTags().contains(tag)) {
					picture.getTags().add(tag);
				}
			}
		}
		picture.setUser(user);
		if(picture.isPersistence()){
			picDao.update(picture);
		}else{
			picDao.save(picture);
		}
	}

	public void addTagToPicture(Long id, String tagName) {
		Picture picture = picDao.findById(id);
		Tag tag = new Tag();
		tag.setName(tagName);
		tag = tagDao.save(tag);
		picture.getTags().add(tag);
		picDao.update(picture);
	}

	public void savePictureComment(PictureComment picComment) {
		this.picCommentDao.save(picComment);
	}

	public void delPicture(Long id){
		Picture picture = picDao.findById(id);
		picDao.delete(picture);
	}

	public void updatePicture(Picture pic){
		picDao.update(pic);
	}

	public Picture getPicture(Long id){
		return picDao.findById(id);
	}

	public Tag addTag(String tagName) {
		Tag tag = new Tag();
		tag.setName(tagName);
		return tagDao.save(tag);
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public PictureDao getPicDao() {
		return picDao;
	}

	public void setPicDao(PictureDao picDao) {
		this.picDao = picDao;
	}

	public TagDao getTagDao() {
		return tagDao;
	}

	public void setTagDao(TagDao tagDao) {
		this.tagDao = tagDao;
	}

	public PictureCommentDao getPicCommentDao() {
		return picCommentDao;
	}

	public void setPicCommentDao(PictureCommentDao picCommentDao) {
		this.picCommentDao = picCommentDao;
	}

}
